package com.ttdev.mmms.controller.updatePwd;

import com.ttdev.mmms.entity.MemberInfo;
import com.ttdev.mmms.entity.StoreInfo;
import com.ttdev.mmms.repository.MemberRepository;
import com.ttdev.mmms.repository.StoreRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;

/**
 * 用户更改密码
 */
@Controller
@RequestMapping("/update")
public class UpdatePwdController {

    @Autowired
    private MemberRepository memberRepository;
    @Autowired
    private StoreRepository storeRepository;

    // 更改商家登陆密码
//    @RequestMapping("/updatePassWord")
//    @ResponseBody
//    public StoreInfo updatePassWord(HttpServletRequest request) {
//        StoreInfo account = (StoreInfo) storeService.getContext();
//        String accountId = account.getPassword();
//        String oldpwd = request.getParameter("sysOldPWInp");
//        String newPWInp = request.getParameter("sysNewPWInp");
//        String conPW = request.getParameter("sysConPW");
//        StoreInfo retKit = UpdatePwdService.updatePwd(accountId, oldpwd, newPWInp,conPW);
//        if(retKit.success) {
//            // 清除session
//            Enumeration<String> em = request.getSession().getAttributeNames();
//            while (em.hasMoreElements()) {
//                request.getSession().removeAttribute(em.nextElement().toString());
//            }
//            request.getSession().invalidate();
//        }
//        return retKit;
//    }
//
//
//    @RequestMapping("/updateMemberPwd/{username}")
//    public ModelAndView updateMemberPwd(@PathVariable("username") String username){
//        MemberInfo memberInfo = memberService.findPwd(username);
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("updatePwd");
//        modelAndView.addObject("memberManage",memberInfo);
//        return  modelAndView;
//    }
    @RequestMapping("memberUpdatePwd")
    public String memberUpdatePwd(){
        return "updatepwd/memberUpdatePwd";
    }
    @RequestMapping("storeUpdatePwd")
    public String storeUpdatePwd(){
        return "updatepwd/storeUpdatePwd";
    }
    @GetMapping("/updatePassWord1")
    public String u1(String iphone, String oldPassword, String newPassword, Model model, HttpSession session){
        MemberInfo memberInfo=(MemberInfo)session.getAttribute("member");
        if (!memberInfo.getIphone().equals(iphone)){
            model.addAttribute("msg", "手机号不正确！");
        }else if (!memberInfo.getPassword().equals(oldPassword)){
            model.addAttribute("msg", "旧密码不正确！");
        }else if (memberInfo.getPassword().equals(newPassword)){
            model.addAttribute("msg", "新密码不能和旧密码一样哦！");
        }else if (memberInfo.getIphone().equals(iphone)&&memberInfo.getPassword().equals(oldPassword)){
            memberRepository.updatePassword(newPassword,memberInfo.getCardId());
            model.addAttribute("msg", "您已重新设置密码，请重新登录！");
            session.removeAttribute("member");
        }
        return "memberUpdatePwd";
    }
    @GetMapping("/updatePassWord2")
    public String u2(String iphone,String oldPassword,String newPassword,Model model,HttpSession session){
        StoreInfo storeInfo=(StoreInfo)session.getAttribute("storeManage");
        if (!storeInfo.getIphone().equals(iphone)){
            model.addAttribute("msg", "手机号不正确");
        }else if (!storeInfo.getPassword().equals(oldPassword)){
            model.addAttribute("msg", "旧密码不正确");
        }else if (storeInfo.getPassword().equals(newPassword)){
            model.addAttribute("msg", "新密码不能和旧密码一样哦！");
        }else if (storeInfo.getIphone().equals(iphone)&&storeInfo.getPassword().equals(oldPassword)){
            storeRepository.updatePassword(newPassword,storeInfo.getStoreName());
            model.addAttribute("msg", "您已重新设置密码，请重新登录！");
            session.removeAttribute("storeManage");
        }
        return "storeUpdatePwd";
    }

}
